<?php
namespace haibao\cms\business\activity;

use  \haibao\cms\data\activity\ActivityPrize  as ActivityPrizeData;
use \haibao\comment\business\BusinessException;
use \haibao\cms\business\activity\activity as ActivityBusiness;

class ActivityPrize extends \haibao\cms\business\Base{
    public static function getActivityPrizeList($filter,$isGetOne = TRUE){
        $activityData = new ActivityPrizeData;
        if($isGetOne){
            return $activityData->getOne($filter);
        }else{
            return $activityData->getAll($filter);
        }
    }
    
    public static function delActivityPrizeById($id, $arrSet){
        self::checkOperation(self::FUNC_CODE_ACTIVITY_PRIZE_DEL);
        if(!$id){
            throw new BusinessException('Id不可以为空！');
        }
        $activityData = new ActivityPrizeData();
        
        $activityPrizeInfo = $activityData->getOneById($id);
        $activityData->updateById($id, $arrSet);
        
        if($activityPrizeInfo){
            \haibao\cms\business\activity\Activity::updateActivityPrizeNumById($activityPrizeInfo->ActivityId,$activityPrizeInfo->Num,1);
        } 
        
        ActivityBusiness::delActivityCache($activityPrizeInfo->ActivityId);
        
        self::saveOperateHistory(self::FUNC_CODE_ACTIVITY_PRIZE_DEL, array($id,$arrSet));
    }
    public static function getActivityPrizeById($id){
        if(!$id){
            throw new BusinessException('Id不可以为空！');
        }
        $activityData = new ActivityPrizeData;
        return $activityData->getOneById($id);
    }
    
    public static function updateActivityPrizeById($editActivityPrizeModel){
        \haibao\cms\business\Base::checkOperation(self::FUNC_CODE_ACTIVITY_PRIZE_EDIT);
        self::checkModelData($editActivityPrizeModel,true);
        $activityData = new ActivityPrizeData;
        $restNum = $activityData->getOneById($editActivityPrizeModel->Id);
        $activityData->updateById($editActivityPrizeModel->Id, $editActivityPrizeModel);
        \haibao\cms\business\activity\Activity::updateActivityPrizeNumById($editActivityPrizeModel->ActivityId,$editActivityPrizeModel->Num,3,$restNum->RestNum);
        ActivityBusiness::delActivityCache($editActivityPrizeModel->ActivityId);
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_ACTIVITY_PRIZE_EDIT, $editActivityPrizeModel->toArray(false));
    }
    
    public static function addActivityPrize($addActivityPrizeModel){
        \haibao\cms\business\Base::checkOperation(self::FUNC_CODE_ACTIVITY_PRIZE_ADD);
        self::checkModelData($addActivityPrizeModel);
        $activityData = new ActivityPrizeData();
        $activityData->add($addActivityPrizeModel);
        \haibao\cms\business\activity\Activity::updateActivityPrizeNumById($addActivityPrizeModel->ActivityId,$addActivityPrizeModel->Num,2);
        ActivityBusiness::delActivityCache($addActivityPrizeModel->ActivityId);
        \haibao\cms\business\Base::saveOperateHistory(self::FUNC_CODE_ACTIVITY_PRIZE_ADD, $addActivityPrizeModel->toArray(false));
    }
    
   private static function checkModelData($model,$isUpdate = FALSE){
       if($isUpdate){
           if(!$model->Id){
               throw new BusinessException('Id不可以为空！');
           }
       }
       if(!$model->Desc){
           throw new BusinessException('Desc不可以为空！');
       }
       
       if(!$model->ImageId){
           throw new BusinessException('ImageId不可以为空！');
       }
       
       if(!$model->Num){
           throw new BusinessException('Num不可以为空！');
       }
       
       if(!$model->Level){
           throw new BusinessException('Level不可以为空！');
       }
   }
}